<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2022/5/26
 * Time: 下午2:28
 */

namespace app\api\controller\v32;

use app\api\controller\v2\Auth;
use app\common\cachemodel\Article;
use app\common\cachemodel\ArticleComment;
use app\common\cachemodel\ArticleCommentReply;
use app\common\cachemodel\GoddessArea as GoddessAreaModel;
use app\common\cachemodel\lucky\AppUserAdornInfo;
use app\common\cachemodel\ZlGoddessState;
use app\common\cachemodel\ZlUserVip;
use app\common\lib\ConfigManage;
use app\common\model\UserPush;
use think\response\Json;

/**
 * 评论和回复
 * @version v2.8.5
 * Class ArticleCommentPost
 * @package app\api\controller\v32
 */
class ArticleCommentPost extends Auth
{
    /**
     * 添加评论
     * @return Json
     * @throws \Exception
     */
    public function comment()
    {
        $id = input('id/d', 0);
        $content = trim(htmlspecialchars(input('param.content/s', '')));
        $type = input('type/d', 0);

        if (empty($content))
        {
            return show(config('code.error'), '评论内容不能为空');
        }
        if (!$id || !in_array($type, [1, 2, 3], true))
        {
            return show(config('code.error'), '请求异常');
        }
        if ($this->user['no_comment'])
        {
            return show(config('code.error'), '评论失败，已被禁止评论');
        }

        switch ($type)
        {
            case 1:
                $articleInfo = current((new Article())->batchGetInfo([$id], ['id']));
                if (!$articleInfo)
                {
                    return show(config('code.error'), '内容不存在');
                }
                break;
            case 2:
                $goddessState = (new ZlGoddessState())->getInfoById($id, ['type']);
                if (!$goddessState)
                {
                    return show(config('code.error'), '内容不存在');
                }
                break;
            case 3:
                // 获取话题信息
                $themeInfo = current((new GoddessAreaModel())->batchGetThemeInfo($id, ['title']));
                if (!$themeInfo)
                {
                    return show(config('code.error'), '内容不存在');
                }
                break;
            default :
                return show(config('code.error'), '内容不存在');
                break;
        }

        $data = [
            'aid' => $id,
            'uid' => (int)$this->user['id'],
            'content' => $content,
            'type' => $type,    //类型 1=>星文评论，2=>女神动态评论，3=>女神专区评论 必传
            'status' => 0   // 状态 0 正常 -1 删除
        ];
        $res = (new ArticleComment())->commentAdd($this->user, $data);

        if ($res['status'])
        {
            // 获取内部官方用户
            $userType = (new ConfigManage())->innerUser((int)($this->user['id'] ?? 0));
            // 获取用户当前装扮信息
            $adorn = (new AppUserAdornInfo())->getUserAdornInfo($this->user['id'], $userType);
            $rData = [
                'id' => $res['id'],
                'avatar' => $this->user['avatar'],
                'nickname' => $this->user['nickname'],
                'time' => '刚刚',
                'content' => htmlspecialchars_decode($content),
                'reply_num' => 0,
                'good_num' => 0,
                'is_good' => 0,
                'vip' => (new ZlUserVip())->getUserVipLevel($this->user['id']),
                'is_owner' => 1,
                'reply_list' => [],
                'avatar_adorn' => $adorn['adorn_info']['reality_img'],
                'badge' => array_column($adorn['badge_info'], 'reality_img'),
            ];

            return show(config('code.success'), '评论成功', $rData);
        }
        return show(config('code.error'), $res['msg']);
    }

    /**
     * 添加回复
     * @return Json
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \app\common\lib\exception\ApiException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function commentReply()
    {
        $commentId = input('comment_id/d', 0);
        $pid = input('pid/d', 0);
        $content = trim(htmlspecialchars(input('param.content/s', '')));

        if (empty($content))
        {
            return show(config('code.error'), '评论内容不能为空');
        }
        if (!$commentId)
        {
            return show(config('code.error'), '评论不存在');
        }
        if ($this->user['no_comment'])
        {
            return show(config('code.error'), '评论失败，已被禁止评论');
        }

        // 评论详情
        $commentInfo = current((new ArticleComment())->batchGetInfo([$commentId], ['uid']));
        if (!$commentInfo)
        {
            return show(config('code.error'), '数据不存在');
        }
        // 判断 用户是否允许回复他
        if ($pid)
        {   // 回复->评论回复
            // 获取回复的 评论回复用户uid
            $replyUid = (new ArticleCommentReply())->getInfoById($pid, ['uid1']);
            if ($replyUid['uid1'] != $this->user['id'])
            {   // 不是回复自己
                $replyStatus = (new UserPush())->getUserReplyStatus((int)$replyUid['uid1']); // 是否允许回复自己 true 允许 false 不允许
                if (!$replyStatus)
                {   // 不允许回复
                    return show(config('code.error'), '对方关闭了评论回复');
                }
            }
        } else
        {   // 回复评论
            if ($commentInfo['uid'] != $this->user['id'])
            {   // 不是自己评论自己事
                $replyStatus = (new UserPush())->getUserReplyStatus((int)$commentInfo['uid']); // 是否允许回复自己 true 允许 false 不允许
                if (!$replyStatus)
                {   // 不允许回复
                    return show(config('code.error'), '对方关闭了评论回复');
                }
            }
        }

        $data = [
            'comment_id' => $commentId,
            'pid' => $pid,
            'content' => $content,
            'uid' => (int)$this->user['id'],
            'status' => 0   // 状态 0 正常 -1 删除
        ];

        $res = (new ArticleCommentReply())->addCommentReply($this->user, $data);

        if ($res['status'])
        {
            // 获取内部官方用户
            $userType = (new ConfigManage())->innerUser((int)($this->user['id'] ?? 0));
            // 获取用户当前装扮信息
            $adorn = (new AppUserAdornInfo())->getUserAdornInfo($this->user['id'], $userType);
            $rData = [
                'id' => $res['id'],
                'avatar' => $this->user['avatar'],
                'nickname1' => $this->user['nickname'],
                'nickname2' => $pid ? $res['nickname2'] : null,
                'time' => '刚刚',
                'content' => htmlspecialchars_decode($content),
                'content_type' => 1,
                'good_num' => 0,
                'is_good' => 0,
                'is_owner' => 1,
                'vip' => (new ZlUserVip())->getUserVipLevel($this->user['id']),
                'avatar_adorn' => $adorn['adorn_info']['reality_img'],
                'badge' => array_column($adorn['badge_info'], 'reality_img'),
            ];
            return show(config('code.success'), '评论成功', $rData);
        }
        return show(config('code.error'), $res['msg']);
    }
}
